package com.example.myapplication.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.myapplication.R;

public class BeiZhuDialog extends Dialog implements View.OnClickListener {

    private TextView dialogBeizhuTv;
    private EditText dialogBeizhuEt;
    private Button dialogBeizhuBtnCancel;
    private Button dialogBeizhuBtnEnsure;
    OnEnsureListener onEnsureListener;


    public  interface  OnEnsureListener{
        public void onEsure();
    }
    //设定接口回调方法
    public void setOnEnsureListener(OnEnsureListener onEnsureListener){
        this.onEnsureListener=onEnsureListener;
    }
    public BeiZhuDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_beizhu);//设置对话框显示布局
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_beizhu_btn_cancel:
                cancel();
                break;
            case R.id.dialog_beizhu_btn_ensure:
                if (onEnsureListener!=null) {
                    onEnsureListener.onEsure();
                }
                break;

        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        dialogBeizhuTv = (TextView) findViewById(R.id.dialog_beizhu_tv);
        dialogBeizhuEt = (EditText) findViewById(R.id.dialog_beizhu_et);
        dialogBeizhuBtnCancel = (Button) findViewById(R.id.dialog_beizhu_btn_cancel);
        dialogBeizhuBtnEnsure = (Button) findViewById(R.id.dialog_beizhu_btn_ensure);
        dialogBeizhuBtnCancel.setOnClickListener(this);
        dialogBeizhuBtnEnsure.setOnClickListener(this::onClick);
    }
    /**
     * 获取输入数据的方法
     */
    public  String getEditText(){
        return dialogBeizhuEt.getText().toString().trim();
    }
    /**
     * 设置Dialog的尺寸和屏幕尺寸一致
     */
//    public void setDialogSize(){
//        //获取当前窗口对象
//        Window window=getWindow();
//        //获取窗口对象的参数
//        WindowManager.LayoutParams attributes = window.getAttributes();
//        //获取屏幕宽度
//        Display d = window.getWindowManager().getDefaultDisplay();
//        //对话框窗口为1屏幕窗口
//        attributes.width=(int)d.getWidth();
//        attributes.gravity= Gravity.BOTTOM;
//        window.setBackgroundDrawableResource(android.R.color.transparent);
//        window.setAttributes(attributes);
//        handler.sendEmptyMessageDelayed(1,150);
//    }
    /* 设置Dialog的尺寸与屏幕尺寸一致*/
    public void setDialogSize(){
        Window window = getWindow();   //获取当前窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();   //获得窗口对象的参数
        Display d = window.getWindowManager().getDefaultDisplay();//获取屏幕宽
        wlp.width = (int) (d.getWidth());//宽度按屏幕大小的百分比设置 ;d.getWidth()这个方法已废弃
        wlp.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);  // 设置窗口的背景资源
        window.setAttributes(wlp);
        handler.sendEmptyMessageDelayed(1,150);
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            //自动弹出软键盘的方法
            InputMethodManager inputMethodManager =(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
        }
    };
}
